package com.jet.cashier.service;

import cn.hutool.core.collection.CollUtil;
import com.jet.cashier.domain.entity.CashierOrderEntity;
import com.jet.cashier.domain.entity.CashierRefundEntity;
import com.jet.cashier.domain.enums.CashierOrderCategory;
import com.jet.cashier.domain.enums.CashierOrderType;
import com.jet.cashier.domain.enums.CashierRefundStatus;
import com.jet.cashier.domain.query.CashierOrderSummaryQuery;
import com.jet.cashier.domain.query.CashierRefundSummaryQuery;
import com.jet.core.utils.AmountUtil;
import com.jet.member.domain.vo.MemberSummaryCashierVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dcl
 * @since 2025/2/21 16:14
 */
@Service
public class CashierMemberOrderService {

    private final CashierOrderService cashierOrderService;
    private final CashierRefundService cashierRefundService;

    public CashierMemberOrderService(CashierOrderService cashierOrderService, CashierRefundService cashierRefundService) {
        this.cashierOrderService = cashierOrderService;
        this.cashierRefundService = cashierRefundService;
    }

    public MemberSummaryCashierVo summaryCashier(long merchantNo, String memberId) {
        MemberSummaryCashierVo vo = new MemberSummaryCashierVo();
        CashierOrderSummaryQuery orderSummaryQuery = new CashierOrderSummaryQuery();
        orderSummaryQuery.setMerchantNo(merchantNo);
        orderSummaryQuery.setMemberId(memberId);
        List<CashierOrderEntity> cashierOrders = cashierOrderService.summaryList(orderSummaryQuery);

        CashierRefundSummaryQuery refundSummaryQuery = new CashierRefundSummaryQuery();
        refundSummaryQuery.setMerchantNo(merchantNo);
        refundSummaryQuery.setMemberId(memberId);
        List<CashierRefundEntity> cashierRefunds = cashierRefundService.summaryList(refundSummaryQuery);

        cashierOrders = cashierOrders.stream()
                .filter(x -> x.getType() == CashierOrderType.AMOUNT.getCode() && x.getCategory() == CashierOrderCategory.GOODS.getCode())
                .collect(Collectors.toList());
        cashierRefunds = cashierRefunds.stream()
                .filter(x -> x.getStatus() == CashierRefundStatus.REFUND_SUBMIT.getCode() || x.getStatus() == CashierRefundStatus.REFUND_FINISH.getCode())
                .filter(x -> x.getType() == CashierOrderType.AMOUNT.getCode() && x.getCategory() == CashierOrderCategory.GOODS.getCode())
                .collect(Collectors.toList());
        long cashierAmount = cashierOrders.stream().mapToLong(x -> x.getRealityAmount() - x.getRefundAmount()).sum();
        long refundAmount = cashierRefunds.stream().mapToLong(CashierRefundEntity::getPayableAmount).sum();

        vo.setCashierAmount(AmountUtil.parseBigDecimal(cashierAmount));
        vo.setCashierTotal(cashierOrders.size());
        vo.setRefundAmount(AmountUtil.parseBigDecimal(refundAmount));
        vo.setRefundTotal(cashierRefunds.size());

        if (CollUtil.isNotEmpty(cashierOrders)) {
            BigDecimal referenceAmount = AmountUtil.divide(vo.getCashierAmount(), BigDecimal.valueOf(vo.getCashierTotal()));
            vo.setReferenceAmount(referenceAmount);
        }
        // 最近下单时间
        cashierOrders.stream().max(Comparator.comparing(CashierOrderEntity::getCashierTime))
                .ifPresent(x -> vo.setLastCashierTime(x.getCashierTime()));
        return vo;
    }
}
